// ------------------------------------------------------------------------------------------
// Licensed by Interprise Solutions.
// http://www.InterpriseSolutions.com
// For details on this license please visit  the product homepage at the URL above.
// THE ABOVE NOTICE MUST REMAIN INTACT.
// ------------------------------------------------------------------------------------------
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using InterpriseSuiteEcommerceCommon.Extensions;
using System;

namespace InterpriseSuiteEcommerceCommon.DTO
{
    [DataContractAttribute]
    public class ProductPricePerUnitMeasure
    {
        public ProductPricePerUnitMeasure(string unitMeasureCode)
        {
            this.code = unitMeasureCode;
        }

        [DataMemberAttribute]
        public string code;

        [DataMemberAttribute]
        public string description;

        [DataMemberAttribute]
        public decimal price;

        [DataMemberAttribute]
        public string priceFormatted;

        [DataMemberAttribute]
        public bool hasPromotionalPrice;

        [DataMemberAttribute]
        public decimal promotionalPrice;

        [DataMemberAttribute]
        public string promotionalPriceFormatted;

        [DataMemberAttribute]
        public decimal unitMeasureQuantity;

        [DataMemberAttribute]
        public decimal freeStock = decimal.Zero;

        [DataMemberAttribute]
        public decimal originalSalePrice = decimal.Zero;

        [DataMemberAttribute]
        public string originalSalePriceFormatted;

        [DataMemberAttribute]
        public bool hasDiscount;

        public static List<ProductPricePerUnitMeasure> GetAll(string itemCode, Customer forCustomer)
        {
            return GetAll(itemCode, forCustomer, false);
        }

        public static List<ProductPricePerUnitMeasure> GetAll(string itemCode, Customer forCustomer, bool hideUntilCart)
        {
            string customerCode = forCustomer.CustomerCode;
            if (forCustomer.IsNotRegistered)
            {
                customerCode = forCustomer.AnonymousCustomerCode;
            }

            var availableUnitMeasures = new List<ProductPricePerUnitMeasure>();
            using (var con = DB.NewSqlConnection())
            {
                con.Open();
                using (var reader = DB.GetRSFormat(con, "exec eCommerceGetProductUnitMeasureAvailability @CustomerCode = {0}, @ItemCode = {1}, @IncludeAllWarehouses = {2}, @Anon = {3}",
                                                        customerCode.ToDbQuote(), itemCode.ToDbQuote(), AppLogic.AppConfigBool("ShowInventoryFromAllWarehouses"), "false"))
                {
                    while (reader.Read())
                    {
                        var pricePerUnitMeasure = new ProductPricePerUnitMeasure(DB.RSField(reader, "UnitMeasureCode"))
                        {
                            freeStock = Decimal.Zero,
                            description = DB.RSField(reader, "UnitMeasureDescription"),
                            unitMeasureQuantity = DB.RSFieldDecimal(reader, "UnitMeasureQuantity")
                        };
                        availableUnitMeasures.Add(pricePerUnitMeasure);
                    }
                }
            }

            var lstUnitMeasureInfo = InterpriseHelper.GetListItemUnitMeasure(itemCode, availableUnitMeasures.Select(m => m.code));

            decimal vat = 0;
            foreach (var unitMeasurePerProduct in availableUnitMeasures)
            {
                decimal salesPrice = Decimal.Zero;
                decimal promotionalPrice = Decimal.Zero;
                bool withVat = AppLogic.AppConfigBool("VAT.Enabled") && forCustomer.VATSettingReconciled == VatDefaultSetting.Inclusive;

                decimal originalSalePrice = decimal.Zero;
                string originalSalePriceFormatted = String.Empty;
                bool hasDiscount = false;

                var item = lstUnitMeasureInfo.FirstOrDefault(c => c.Code == unitMeasurePerProduct.code);

                //check if has discount
                salesPrice = InterpriseHelper.GetSalesPriceAndTax(
                                                                  forCustomer.CustomerCode, itemCode, forCustomer.CurrencyCode, decimal.One, unitMeasurePerProduct.code, withVat, 
                                                                   ref promotionalPrice, ref vat, item, null, ref originalSalePrice, ref hasDiscount);

                string salesPriceFormatted = forCustomer.FormatBasedOnMyCurrency(salesPrice);
                string promotionalPriceFormatted = forCustomer.FormatBasedOnMyCurrency(promotionalPrice);
                // added additional logic to check if the promotional price is greater than the sales price. if it is then don't display the promotional price
                unitMeasurePerProduct.hasPromotionalPrice = promotionalPrice > Decimal.Zero && promotionalPrice < salesPrice;

                if (hideUntilCart ||
                    (AppLogic.AppConfigBool("WholesaleOnlySite") &&
                        forCustomer.DefaultPrice != Interprise.Framework.Base.Shared.Const.BUSINESS_TYPE_WHOLESALE))
                {
                    salesPrice = Decimal.Zero;
                    salesPriceFormatted = String.Empty;
                    promotionalPrice = Decimal.Zero;
                    promotionalPriceFormatted = String.Empty;
                    originalSalePrice = decimal.Zero;
                    hasDiscount = false;
                    originalSalePriceFormatted = String.Empty;
                }

                unitMeasurePerProduct.price = salesPrice;
                unitMeasurePerProduct.priceFormatted = salesPriceFormatted;

                unitMeasurePerProduct.promotionalPrice = promotionalPrice;
                unitMeasurePerProduct.promotionalPriceFormatted = promotionalPriceFormatted;

                // check if has discount
                unitMeasurePerProduct.hasDiscount = hasDiscount;
                unitMeasurePerProduct.originalSalePrice = originalSalePrice;
                originalSalePriceFormatted = forCustomer.FormatBasedOnMyCurrency(originalSalePrice);
                unitMeasurePerProduct.originalSalePriceFormatted = originalSalePriceFormatted;
                
            }

            return availableUnitMeasures;
        }
    }
}



